from decimal import Decimal
from typing import Union, List
from fastapi import Query
from pydantic import BaseModel, Field
from apps.api.schemas.agent.know.dataset_schema import KnowDatasetAnnex


class RobotJournalSearchIn(BaseModel):
    """ 日志搜索参数 """
    page_no: int = Query(default=1, gt=0, description="当前页码")
    page_size: int = Query(default=15, gt=0, le=200, description="每页条数")
    bot: str = Query(..., min_length=1, max_length=32, description="智能体编号")
    scene: str = Field(default="", pattern=r"^(normal|test|web|api|)$", description="来源场景")
    start_time: Union[int, str, None] = Query(default=None, description="开始时间")
    end_time: Union[int, str, None] = Query(default=None, description="结束时间")


class RobotJournalDeleteIn(BaseModel):
    ids: List[int] = Field(..., description="记录ID")
    bot: str = Field(..., min_length=1, max_length=32, description="智能体编号")


class RobotJournalCorrectsIn(BaseModel):
    bot: str = Field(..., min_length=1, max_length=32, description="智能体编号")
    kb: str = Field(..., min_length=1, max_length=32, description="知识库编号")
    fid: str = Field(..., min_length=1, max_length=32, description="文档的编号")
    question: str = Field(..., min_length=1, max_length=65535, description="问题")
    answer: str = Field(..., max_length=65535, description="内容")
    annex: List[KnowDatasetAnnex] = Field(default=[], description="附件")


"""--------------- Separator ---------------"""


class RobotJournalListVo(BaseModel):
    """ 日志记录列表VO """
    id: int = Field(description="ID")
    scene: str = Field(description="场景: [normal,test,web,api]")
    question: str = Field(description="提问问题")
    answer: str = Field(description="答复内容")
    thinks: str = Field(description="思考过程")
    annex: list = Field(description="提问附件")
    quotes: list = Field(description="引用内容")
    context: list = Field(description="上下文值")
    use_tokens: int = Field(description="消耗tokens")
    use_points: int = Field(description="消耗积分")
    user_delete: bool = Field(description="用户删除")
    task_time: str = Field(description="执行耗时")
    create_time: str = Field(description="创建时间")
    ip: str = Field(description="来源IP")
    user: dict = Field(description="用户信息")

    class Config:
        json_schema_extra = {
            "example": {
                "id": 17,
                "scene": "normal",
                "question": "你好",
                "answer": "你好！很高兴看到你对飞机的飞行原理感兴趣。",
                "thinks": "思考过程。",
                "annex": [{
                    "id": "53b180900a2947efae342a39ebb08430",
                    "file_type": "file",
                    "file_name": "aa.docx",
                    "file_size": 22950,
                    "file_ext": "docx",
                    "file_path": "https://www.a.com/files/aa.docx"
                }],
                "quotes": [
                    {
                        "uuid": "0bdbaa93-7905-4ae6-a220-79c8f592cabe",
                        "archive_id": 1,
                        "question": "飞机的飞行原理",
                        "answer": "这涉及到动力学了",
                        "score": 0.48842
                    }
                ],
                "context": [
                    {
                        "role": "assistant",
                        "content": "你好！根据你提供的文档内容"
                    },
                    {
                        "role": "user",
                        "content": "{\"document\": \"飞机的飞行原理\\n这涉及到动力学了\\n\", \"question\": \"你好\"}"
                    }
                ],
                "use_tokens": 1389,
                "use_points": 3,
                "user_delete": False,
                "task_time": "7.37s",
                "create_time": "2025-01-23 16:40:14",
                "ip": "127.0.0.1",
                "user": {
                    "sn": "52778960",
                    "nickname": "52778960",
                    "avatar": "http://0.0.0.0:8200/static/images/default_avatar.png"
                }
            }
        }
